package com.ayi.servlet.base; /**
 * @author Ayingzz
 * @create 2021/11/29 11:15
 */

import com.ayi.bean.User;
import com.ayi.dao.cartdao.AddCart;
import com.ayi.dao.cartdao.QueryCart;
import com.ayi.dao.userdao.QueryUser;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        System.out.println(username);

        String password = request.getParameter("password");
        System.out.println(password);

        User user = new QueryUser().queryUserByUserName(username);

        if (user != null && password.equals(user.getPassword())) {
            if (user.isStop()) {
                PrintWriter out = response.getWriter();
                out.print("您的账号被官方禁用，请联系管理员...3秒后返回主页面");
                response.setHeader("refresh", "3;url=" + request.getContextPath() + "/index.jsp");
                return;
            }
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect("IndexServlet");
        } else {
            PrintWriter out = response.getWriter();
            out.print("用户不存在，或密码错误，请重新登录...3秒后返回登录页面");
            response.setHeader("refresh", "3;url=" + request.getContextPath() + "/login.jsp");
            return;
        }
    }
}
